package com.clhbolg.consumer;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.clhbolg.constant.RabbitMQConstant;
import com.clhbolg.model.dto.EmailDTO;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;

/**
 * @Author cailinhu
 * @Description TODO
 * @Date 2023/3/1 10:50
 * @Version 1.0
 */
@RabbitListener(queues = RabbitMQConstant.EMAIL_QUEUE)
@Component
public class RabbitConsumer {
    @Resource
    private TemplateEngine templateEngine;
    @Autowired
    MailAccount mailAccount;

    @RabbitHandler
    public void sendEmail(EmailDTO emailDTO){
        Context context = new Context();
        context.setVariables(emailDTO.getCommentMap());
        String process = templateEngine.process(emailDTO.getTemplate(), context);
        /**
         * 针对用户注册的信息发送
         */
        MailUtil.send(mailAccount, CollUtil.newArrayList( emailDTO.getEmail()), emailDTO.getSubject(), process, true);
    }
}
